<template>
  <div class="attestation-wrap">
    <el-form class="form">
      <el-form-item label-width="100px" label="姓名：">
        <el-input v-model="username" type="text"/>
      </el-form-item>
      <el-form-item label-width="120px" label="手机号：">
        <el-input v-model="phone" type="text"/>
      </el-form-item>
      <el-form-item label-width="100px" label="认证状态：">
        <el-select v-model="state" placeholder="请选择">
          <el-option
            v-for="item in states"
            :key="item.value"
            :label="item.label"
            :value="item.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label-width="50px">
        <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="tableData" style="width: 100%" border>
      <el-table-column prop="index" label="序号" width="100"/>
      <el-table-column prop="username" label="姓名" min-width="200"/>
      <el-table-column prop="phone" label="手机号" width="180"/>
      <el-table-column prop="optdate" sortable label="提交时间" width="200"/>
      <el-table-column prop="authdate" label="审核时间" width="200"/>
      <el-table-column prop="ideno" label="证件号码" width="200"/>
      <el-table-column prop="fronturl" label="正面图片" min-width="180">
        <template slot-scope="scope">
          <div class="image-wrap">
            <img :src="`${baseUrl}/common/viewImage?imagepath=${scope.row.fronturl}`" :preview="scope.$index" preview-text="" alt="">
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="reverseurl" label="反面图片" min-width="180">
        <template slot-scope="scope">
          <div class="image-wrap">
            <img :src="`${baseUrl}/common/viewImage?imagepath=${scope.row.reverseurl}`" :preview="scope.$index" preview-text="" alt="">
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="state" label="状态" width="100">
        <template slot-scope="scope">
          <el-tag :type="scope.row.state === 2 ? 'success': 'danger'">{{ scope.row.state === 1 ? '待审核': scope.row.state === 2? '已通过': '未通过' }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="remarks" label="备注" min-width="400">
        <template slot-scope="scope">
          <template v-if="scope.row.edit">
            <el-row class="edit-box">
              <el-input v-model="scope.row.remarks" style="margin-right:10px" class="edit-input" size="small"/>
              <el-button class="cancel-btn" size="small" icon="el-icon-refresh" type="warning" @click="cancelEdit(scope.row)">取消</el-button>
            </el-row>
          </template>
          <div v-else style="height:100px;cursor: pointer;" @dblclick="scope.row.edit=!scope.row.edit">{{ scope.row.remarks }}</div>
        </template>
      </el-table-column>
      <el-table-column prop="switch" label="操作" width="200">
        <template slot-scope="scope">
          <el-button type="primary" @click="handleDelete(scope)">审核</el-button>
          <el-button v-if="scope.row.edit" type="success" icon="el-icon-circle-check-outline" @click="confirmEdit(scope.row)">确认</el-button>
          <el-button v-else type="primary" icon="el-icon-edit" @click="scope.row.edit=!scope.row.edit">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>10" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
    <el-dialog
      :visible.sync="centerDialogVisible"
      title="审核"
      width="30%"
      center>
      <el-row>
        <el-radio v-model="radio" label="2">通过</el-radio>
        <el-radio v-model="radio" label="3">未通过</el-radio>
      </el-row>
      <span slot="footer" class="dialog-footer">
        <el-button @click="centerDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleAttestation">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import Pagination from '@/components/Pagination'
import { agencyId, agencyIdState } from '@/api/user'
import base from '@/api/base'
export default {
  name: 'Publish',
  components: {
    Pagination
  },
  data() {
    return {
      baseUrl: base.pro,
      currentIndex: 0,
      agencyName: '',
      phone: '',
      statesValue: '',
      city: '',
      radio: '2',
      states: [
        {
          value: '',
          label: '全部'
        },
        {
          value: 1,
          label: '未审核'
        },
        {
          value: 2,
          label: '审核通过'
        },
        {
          value: 3,
          label: '审核未通过'
        }
      ],
      typeValue: '',
      type: [
        {
          value: '选项一',
          label: '全部'
        },
        {
          value: '选项二',
          label: '个人认证'
        },
        {
          value: '选项三',
          label: '企业认证'
        }
      ],
      listQuery: {
        page: 1,
        limit: 10
      },
      total: 0,
      centerDialogVisible: false,
      list: [],
      state: '',
      iDisplayStart: 0,
      iDisplayLength: 10,
      username: ''
    }
  },
  computed: {
    tableData() {
      const result = []
      this.list.map((item, index) => {
        // const idx = this.type.findIndex((subitem) => {
        //   return item.typecode === subitem.code
        // })
        const flag = item.state === 1
        item = Object.assign({}, item, { index: `${(this.listQuery.page - 1) * this.listQuery.limit + 1 + index}`, edit: false, leading: flag })
        result.push(item)
      })
      return result
    }
  },
  created() {
    this.getAgencyIdList()
  },
  methods: {
    handleDelete(scope) {
      this.centerDialogVisible = true
      this.currentIndex = scope.$index
    },
    getList(prop) {
      this.listQuery.page = prop.page
      this.listQuery.limit = prop.limit
      this.iDisplayStart = (prop.page - 1) * prop.limit
      this.iDisplayLength = prop.limit
      this.getAgencyIdList()
    },
    hanleCheckCity() {
      const { city } = this
      if (city.length) {
        this.areaname = `${city[0]}${city[1]}${city[2]}`
      } else {
        this.areaname = ''
      }
    },
    getAgencyIdList() {
      const { state, iDisplayStart, iDisplayLength, username, phone } = this
      agencyId({ state, iDisplayStart, iDisplayLength, username, phone })
        .then((res) => {
          const data = res.data
          this.list = data
          this.total = res.total
        }).catch((e) => {
          this.$message.error({
            message: '请稍后再试',
            duration: 1000
          })
        })
    },
    handleSearch() {
      this.getAgencyIdList()
    },
    handleAttestation() {
      const index = this.currentIndex
      this.centerDialogVisible = false
      const id = this.tableData[index].userid
      const state = this.radio
      const { remarks, username } = this.tableData[index]
      agencyIdState({ state, id, remarks, username })
        .then((res) => {
          this.$message.success({
            message: '审核完成',
            duration: 1000
          })
          this.getAgencyIdList()
        }).catch((e) => {
          this.$message.error({
            message: '请稍后再试',
            duration: 1000
          })
        })
    },
    cancelEdit(params) {
      params.edit = false
    },
    confirmEdit(params) {
      params.edit = false
    }
  }
}
</script>
<style lang="scss" scoped>
.attestation-wrap {
  width: 100%;
  padding: 20px;
  box-sizing: border-box;
  .form {
    display: flex;
    flex-wrap: wrap;
  }
  .switch-btn {
    margin-top: 20px;
    margin-bottom: 20px;
    margin-left: 20px;
  }
  .image-wrap {
    width: 100px;
    height: 100px;
    img {
      width: 100%;
      height: 100%;
    }
  }
  .edit-box{
    display: flex;
  }
}
</style>
